/*
 * Universidade Federal Fluminense
 * Instituto de Computação
 * Prof. Pedro Braconnot Velloso
 * Sistemas Distribuídos - Turma 2011/2
 * Author: Gabriel Almeida de Oliveira
 * Description: Auxilliary Functions Header
 */

#ifndef _FUNCTIONS_
#define _FUNCTIONS_

	#include "const.h"
	#include "md5.h"

	typedef struct element{
		struct sockaddr_in address;
		char *msg;
		char *hash;
		int level;
		struct element *next, *prev;
	} packet;
	
	struct arguments{
		char *hostname;
		packet *peer_list;
	};
		
	void DieWithError(char *msg);									// Error handling function
	char *substring(char *string, int size);
	char *calculate_hash(char *message);
    unsigned int ELFHash(char* str, unsigned int len);			// generate a hash of length len using string str, using ELF Hash Algorithm
	void getContribution(char* hash, char* newHash, int numPeers); 	// Truncate the hash based on the number of peers
	void completeWithZeros(char* hash); 								// Complete the hash with zeros, so it has 32 characters

	//Linked List Functions
	void append_node(packet *node);
	void insert_after(packet *node, packet *after);
	void remove_node(packet *node);
	int count_nodes();
	packet *search_by_address(char* ip); 	//IMPLEMENTAR 
	packet *getHead();
	packet *getTail();
	
#endif